#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

case "${OSTYPE}" in
    msys*) python="winpty python";;
    *) python="python";;
esac

venv() {
    env_dir=./third_party/env/pytorch
    [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
    case "${OSTYPE}" in
        msys*) source ${env_dir}/Scripts/activate;;
        *) source ${env_dir}/bin/activate;;
    esac
    ${python} -m pip install --quiet --upgrade pip requests
}

clean() {
    echo "pytorch clean"
    rm -rf "./third_party/source/caffe2"
    rm -rf "./third_party/source/pytorch"
}

sync() {
    echo "pytorch sync"
    [ -d "./third_party/source/pytorch" ] || git clone --quiet https://github.com/pytorch/pytorch.git "./third_party/source/pytorch"
    git -C "./third_party/source/pytorch" pull --quiet --prune
    mkdir -p "./third_party/source/caffe2/proto"
    curl --silent --location --output "./third_party/source/caffe2/proto/caffe2.proto" "https://github.com/pytorch/pytorch/raw/5e69e11d098a2cfccc8a59377c431e9c71cab9a8/caffe2/proto/caffe2.proto"
    curl --silent --location --output "./third_party/source/caffe2/proto/torch.proto" "https://github.com/pytorch/pytorch/raw/5e69e11d098a2cfccc8a59377c431e9c71cab9a8/caffe2/proto/torch.proto"
}

install() {
    echo "pytorch install"
    venv
    ${python} -m pip install --quiet --upgrade wheel
    ${python} -m pip install --quiet --upgrade torch torchvision torchaudio --pre --index-url https://download.pytorch.org/whl/nightly/cpu
    # ${python} -m pip install --quiet --upgrade torch-neuron --index-url https://pip.repos.neuron.amazonaws.com
    deactivate
}


schema() {
    echo "pytorch schema"
    [[ $(grep -U $'\x0D' ./source/caffe2-proto.js) ]] && crlf=1
    node ./tools/protoc.js --binary --text --root caffe2 --out ./source/caffe2-proto.js ./third_party/source/caffe2/proto/caffe2.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/caffe2-proto.js ./source/caffe2-proto.js
    fi
    [[ $(grep -U $'\x0D' ./source/pytorch-proto.js) ]] && crlf=1
    node ./tools/protoc.js --json --root pytorch --out ./source/pytorch-proto.js --path ./third_party/source ./third_party/source/caffe2/proto/torch.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/pytorch-proto.js ./source/pytorch-proto.js
    fi
    [[ $(grep -U $'\x0D' ./source/pytorch-schema.js) ]] && crlf=1
    node ./tools/flatc.js --root torch --out ./source/pytorch-schema.js ./third_party/source/pytorch/torch/csrc/jit/serialization/mobile_bytecode.fbs
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/pytorch-schema.js ./source/pytorch-schema.js
    fi
}

metadata() {
    echo "pytorch metadata"
    [[ $(grep -U $'\x0D' ./source/pytorch-metadata.json) ]] && crlf=1
    venv
    ${python} ./tools/pytorch_script.py
    deactivate
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/pytorch-metadata.json ./source/pytorch-metadata.json
    fi
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "install") install;;
        "sync") sync;;
        "schema") schema;;
        "metadata") metadata;;
    esac
done
